from django.contrib import admin
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
from django.contrib.auth.models import User
from django.utils.html import format_html
from .models import UserProfile


class UserProfileInline(admin.StackedInline):
    """用户资料内联编辑"""
    model = UserProfile
    can_delete = False
    verbose_name_plural = '用户资料'
    fields = ('avatar', 'phone', 'department', 'position')
    extra = 0


class UserAdmin(BaseUserAdmin):
    """自定义用户管理"""
    inlines = (UserProfileInline,)
    list_display = (
        'username', 'email', 'get_full_name', 'get_department', 
        'get_position', 'is_active', 'is_staff', 'date_joined'
    )
    list_filter = (
        'is_active', 'is_staff', 'is_superuser', 'date_joined',
        'profile__department'
    )
    search_fields = ('username', 'email', 'first_name', 'last_name')
    ordering = ('-date_joined',)
    
    fieldsets = BaseUserAdmin.fieldsets + (
        ('扩展信息', {
            'fields': (),
            'description': '用户扩展信息在下方的用户资料部分进行编辑'
        }),
    )

    def get_full_name(self, obj):
        """获取用户全名"""
        if hasattr(obj, 'profile'):
            return obj.profile.get_full_name()
        return obj.username
    get_full_name.short_description = '姓名'

    def get_department(self, obj):
        """获取部门"""
        if hasattr(obj, 'profile') and obj.profile.department:
            return obj.profile.department
        return '-'
    get_department.short_description = '部门'

    def get_position(self, obj):
        """获取职位"""
        if hasattr(obj, 'profile') and obj.profile.position:
            return obj.profile.position
        return '-'
    get_position.short_description = '职位'


@admin.register(UserProfile)
class UserProfileAdmin(admin.ModelAdmin):
    """用户资料管理"""
    list_display = (
        'user', 'get_username', 'phone', 'department', 
        'position', 'get_avatar_preview', 'created_at'
    )
    list_filter = ('department', 'position', 'created_at')
    search_fields = (
        'user__username', 'user__email', 'user__first_name', 
        'user__last_name', 'phone', 'department', 'position'
    )
    readonly_fields = ('created_at', 'updated_at')
    ordering = ('-created_at',)
    
    fieldsets = (
        ('基本信息', {
            'fields': ('user', 'avatar', 'phone')
        }),
        ('工作信息', {
            'fields': ('department', 'position')
        }),
        ('时间信息', {
            'fields': ('created_at', 'updated_at'),
            'classes': ('collapse',)
        }),
    )

    def get_username(self, obj):
        """获取用户名"""
        return obj.user.username
    get_username.short_description = '用户名'

    def get_avatar_preview(self, obj):
        """获取头像预览"""
        if obj.avatar:
            return format_html(
                '<img src="{}" width="30" height="30" style="border-radius: 50%;" />',
                obj.avatar.url
            )
        return '无头像'
    get_avatar_preview.short_description = '头像'


# 重新注册User模型
admin.site.unregister(User)
admin.site.register(User, UserAdmin)